package com.lmx.repoDomain.repository;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.HealthAnalysisService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.HealthAnalysis;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

@JsonApiResourceRepository(HealthAnalysis.class)
@Component
public class HealthAnalysisRepository extends BaseRepository<HealthAnalysis> {
	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(HealthAnalysis.class);
	}

	@Autowired
	private HealthAnalysisService srv;

	@Override
	@JsonApiFindAll
	public Iterable<HealthAnalysis> findAll(final QueryParams requestParams) {
		Map<String, String> filter = CommonUtil
				.getKeyMapByFilter(requestParams);
		String isLast = filter.get("isLast");
		if (StringUtils.isNotBlank(isLast) && "1".equals(isLast)) {
			if (!filter.containsKey("customer.id")) {
				throw new BusinessException("",
						ICommonConstants.HEALTH_ANALUSIS_ERROR.CUSTOMER_NULL
								.lable(),
						"customer.id can not be null");
			}
			return this.srv.findAll(requestParams,
					Long.valueOf(filter.get("customer.id")));
		} else {
			return super.findAll(requestParams);
		}
	}
}
